/*====================================================================================
                                                                     
		Financial Constraints and Propagation of Shocks in Production Networks
			    By Demir-Javorcik-Michalski-Ors
  
*====================================================================================*/	

clear all
set more off
log close _all
set matsize 5000

cd "\data\" // Add your directory here

/***************************************
   Imports data
***************************************/
import delimited "Microveri_20062014_t.csv",  delimiter(";") 

*Identify trade under processing regime
gen IPRM=1  if rejım==4051 | rejım==4100 | rejım==4123 | rejım==4171 | rejım==4191 | rejım==4251 |  rejım==5100 | rejım==5123 | rejım==5141 | rejım==5171

sort ulke
merge ulke using "TUIK-country codes.dta" 
drop if _merge==2
drop if country_code==""
drop if country_code=="TUR"
drop _merge ulke

*Identify financing types subject to RUSF
gen RUSF=1(odeme_seklı==1 | odeme_seklı==6 | odeme_seklı==14 | odeme_seklı==15 | odeme_seklı==16)

gen hs6=floor(tarıfe8/100)

collapse (sum) dolar,by(IPRM yıl ıd hs6 country_code odeme_seklı RUSF)

rename yıl year
rename ıd firm_ID
rename odeme_seklı finterm

save "TSI_Imports.dta", replace 


/***************************************
  Other firm-level data
***************************************/
forvalues i=2007(1)2014 {
use "Census_`i'.dta",clear
rename id firm_ID
gen year=`i'
keep firm_ID year ort_ucretli personel_odeme s0520 s0412  s0401 s0402 s0406 s0407 
for X in any personel_odeme s0520 s0412 s0401 s0402 s0406 s0407 : replace X=0 if X==.
rename ort_ucretli employment
rename personel_odeme wages
gen materials=s0401+s0402 
gen fincost=s0412
rename  s0520 sales
keep firm_ID year sales employment  materials wages fincost 
destring firm_ID,replace
sort firm_ID
save temp`i'.dta,replace
}

use "Census_2011.dta",clear
rename id firm_ID
rename nace2_kod nace4
keep firm_ID  nace4 Il_Kodu
destring nace4,replace
gen nace=floor(nace4/100)
rename Il_Kodu province_code 
destring firm_ID,replace
sort firm_ID
save tempid.dta,replace

/***************************************
 Construct firm-level exposure
***************************************/
use "TSI_Imports",clear

drop if IPRM==1 //keep ordinary imports only
keep if year==2010
collapse (sum) dolar ,by(firm_ID RUSF)

reshape wide dolar,i(firm_ID) j(RUSF)
for X in any 0 1: replace dolarX=0 if dolarX==.

destring firm_ID,replace
sort firm_ID
merge firm_ID using temp2010
drop if _merge==2
drop _merge

gen FirmExposure=1.50*dolar1/(materials+wages)
drop if FirmExposure==.

keep firm_ID FirmExposure
sort firm_ID
save tempfirmexp.dta,replace

/***************************************
Construct bartik exposure
***************************************/

use "TSI_Imports",clear

drop if IPRM==1
keep if year==2010
rename hs6 HS6
collapse (sum) dolar ,by(HS6 country_code RUSF)

reshape wide dolar,i(HS6 country_code) j(RUSF)
for X in any 0 1: replace dolarX=0 if dolarX==.
gen VarietyExposure=dolar1/(dolar0+dolar1)

keep HS6 country_code VarietyExposure
sort HS6 country_code
save tempvarietyexp.dta,replace

use "TSI_Imports",clear

drop if IPRM==1
keep if year==2010
rename hs6 HS6
collapse (sum) dolar ,by(firm_ID country_code HS6)
bys firm_ID,sort: egen imports=sum(dolar)

sort HS6 country_code
merge HS6 country_code using tempvarietyexp.dta
drop _merge 

destring firm_ID,replace
sort firm_ID
merge firm_ID using temp2010
drop if _merge==2
drop _merge

gen BartikExposure=1.50*VarietyExposure*dolar/(materials+wages)

collapse (sum) BartikExposure,by(firm_ID)

destring firm_ID, replace
sort firm_ID
save tempbartik.dta,replace


/***************************************
Construct yearly firm-level share of RUSF-affected imports
***************************************/

use "TSI_Imports",clear

drop if IPRM==1
collapse (sum) dolar ,by(firm_ID RUSF year)

reshape wide dolar,i(firm_ID year) j(RUSF)
for X in any 0 1: replace dolarX=0 if dolarX==.

gen RUSFShr_fy=dolar1/(dolar0+dolar1)

destring firm_ID,replace
keep firm_ID year RUSFShr_fy
sort firm_ID year
save tempyexp.dta,replace

/***************************************
Construct yearly firm-level share of CIA-based imports
***************************************/
use "TSI_Imports",clear

drop if IPRM==1
gen CIA=finterm==2
collapse (sum) dolar ,by(firm_ID CIA year)

reshape wide dolar,i(firm_ID year) j(CIA)
for X in any 0 1: replace dolarX=0 if dolarX==.

gen CIAShr_fy=dolar1/(dolar0+dolar1)

destring firm_ID,replace
keep firm_ID year CIAShr_fy
sort firm_ID year
save tempycia.dta,replace

/***************************************
Construct yearly firm-level share of Std LC-based imports
***************************************/
use "TSI_Imports",clear

drop if IPRM==1
gen SLC=finterm==12
collapse (sum) dolar ,by(firm_ID SLC year)

reshape wide dolar,i(firm_ID year) j(SLC)
for X in any 0 1: replace dolarX=0 if dolarX==.

gen SLCShr_fy=dolar1/(dolar0+dolar1)

destring firm_ID,replace
keep firm_ID year SLCShr_fy
sort firm_ID year
save tempyslc.dta,replace

/***************************************
Construct yearly variety-level share of RUSF-affected imports
***************************************/ 
use "TSI_Imports",clear

drop if IPRM==1
rename hs6 HS6
collapse (sum) dolar ,by(HS6 country_code year RUSF)

reshape wide dolar,i(HS6 country_code year) j(RUSF)
for X in any 0 1: replace dolarX=0 if dolarX==.
gen RUSFShr_vy=dolar1/(dolar0+dolar1)

keep HS6 country_code year RUSFShr_vy
sort HS6 country_code year
save tempyvar.dta,replace

/***************************************
Construct firm-year-level imports 
***************************************/ 
use "TSI_Imports",clear

drop if IPRM==1
collapse (sum) dolar ,by(firm_ID year)
rename dolar Imports

destring firm_ID,replace
sort firm_ID year
save tempimp,replace

/***************************************
Construct firm-year-level exposure (processing regime) 
***************************************/
use "TSI_Imports",clear

drop if IPRM==0
keep if year==2010
collapse (sum) dolar ,by(firm_ID RUSF)

reshape wide dolar,i(firm_ID) j(RUSF)
for X in any 0 1: replace dolarX=0 if dolarX==.

destring firm_ID,replace
sort firm_ID
merge firm_ID using temp2010
drop if _merge==2
drop _merge

gen FirmExposure=1.50*dolar1/(materials+wages)

keep firm_ID FirmExposure
sort firm_ID
save tempexpplacebo.dta,replace

/***************************************
Construct Bartik exposure (processing regime) 
***************************************/

use "TSI_Imports",clear

drop if IPRM==0
keep if year==2010
rename hs6 HS6
collapse (sum) dolar ,by(HS6 country_code RUSF)

reshape wide dolar,i(HS6 country_code) j(RUSF)
for X in any 0 1: replace dolarX=0 if dolarX==.
gen VarietyExposure=dolar1/(dolar0+dolar1)

keep HS6 country_code VarietyExposure
sort HS6 country_code
save tempvarplacebo.dta,replace

use "TSI_Imports",clear

drop if IPRM==1
keep if year==2010
rename hs6 HS6
collapse (sum) dolar ,by(firm_ID country_code HS6)
bys firm_ID,sort: egen imports=sum(dolar)

sort HS6 country_code
merge HS6 country_code using tempvarplacebo.dta
drop _merge 

destring firm_ID,replace
sort firm_ID
merge firm_ID using temp2010
drop if _merge==2
drop _merge

gen BartikExposure=1.50*VarietyExposure*dolar/(materials+wages)

collapse (sum) BartikExposure  ,by(firm_ID)

destring firm_ID, replace
sort firm_ID
save tempbartikplacebo.dta,replace


/***************************************
Merge all data
***************************************/
use temp2007.dta,clear
for X in num 2008/2014: append using tempX

sort firm_ID
merge firm_ID using tempfirmexp
drop _merge

sort firm_ID
merge firm_ID using tempbartik
drop _merge

sort firm_ID year
merge firm_ID year using tempyexp
drop _merge

sort firm_ID year
merge firm_ID year using tempycia
drop _merge

sort firm_ID year
merge firm_ID year using tempyslc
drop _merge

sort firm_ID year
merge firm_ID year using tempimp
drop _merge

fillin firm_ID year

replace Imports=0 if Imports==.
gen impshr=.
for X in any 1.3 1.29 1.55 1.50 1.67 1.79 1.90 2.19 \ Y in num 2007/2014: replace impshr=X*Imports/(materials+wages) if year==Y
gen matshr=materials/(materials+wages) 
sort firm_ID year
for X in any  Imports sales RUSFShr_fy CIAShr_fy SLCShr_fy  impshr matshr : gen X_0=X if year==2011
for X in any  Imports sales RUSFShr_fy CIAShr_fy SLCShr_fy  impshr matshr : bys firm_ID,sort: egen mX_0=mean(X_0) 
for X in any  Imports sales RUSFShr_fy CIAShr_fy SLCShr_fy  impshr matshr : replace X_0=mX_0 if X_0==.
for X in any  Imports sales RUSFShr_fy CIAShr_fy SLCShr_fy  impshr matshr : drop mX_0

sort firm_ID year
for X in any  Imports sales:  gen ChgX=ln(X/X_0)
for X in any RUSFShr_fy CIAShr_fy SLCShr_fy  impshr matshr : gen ChgX=X-X_0

sort firm_ID
merge firm_ID using "tempid.dta"
keep if _merge==3
drop _merge

gen nace2=floor(nace/100)
keep if nace2>9 & nace2<34 //Keep manufacturing firms
egen sr=group(nace4 province_code)

gen Emp0=employment if year==2010
gen ImpInt0=1.5*Imports/(sales ) if year==2010
gen ImpShr0=impshr if year==2010
gen LiqMeasure0=fincost/sales  if year==2010
for X in any Emp0 ImpInt0 LiqMeasure0 ImpShr0: bys firm_ID,sort: egen mX=mean(X)
for X in any Emp0 ImpInt0 LiqMeasure0 ImpShr0: replace X=mX if X==.
for X in any Emp0 ImpInt0 LiqMeasure0 ImpShr0: drop mX
gen lnEmp0=ln(Emp0)

bys nace4,sort: egen mLiqMeasure0=mean(LiqMeasure0)
gen LiqCons=(LiqMeasure0>mLiqMeasure0 & LiqMeasure0!=.)
replace LiqCons=. if LiqMeasure0==.

for X in any FirmExposure BartikExposure ImpShr0 ImpInt0: replace X=0 if X==. // For non-importers, replace exposure and import share with zero

gen ChgTau_Firm=FirmExposure*ln((1+0.06)/(1+0.03))
gen ChgTau_Bartik=BartikExposure*ln((1+0.06)/(1+0.03))


save tempall.dta,replace


/***************************************
   Share of USD denomonated imports
***************************************/
import delimited "Microveri_20062014_t.csv",  delimiter(";") 

*Drop processing imports
drop  if rejım==4051 | rejım==4100 | rejım==4123 | rejım==4171 | rejım==4191 | rejım==4251 |  rejım==5100 | rejım==5123 | rejım==5141 | rejım==5171

keep if yıl==2011 // Keep year=2011

sort ulke
merge ulke using "TUIK-country codes.dta" 
drop if _merge==2
drop if country_code==""
drop if country_code=="TUR"
drop _merge ulke

gen usd=(doviz_kodu==400) // Identify flows denominated in USD

gen hs6=floor(tarıfe8/100)

collapse (sum) dolar,by(usd hs6 country_code )

reshape wide dolar,i(hs6 country_code) j(usd)
for X in anu 0 1: replace dolarX=0 if dolarX==.

gen usdshare=dolar1/(dolar0+dolar1)

keep country_code HS6 usdshare
sort country_code HS6

save "usd.dta",replace
